package com.ewanghuiju.app.ui.login.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.App;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.BaseActivity;
import com.ewanghuiju.app.base.contract.login.PhoneLoginContract;
import com.ewanghuiju.app.model.bean.response.LoginResponBean;
import com.ewanghuiju.app.model.bean.response.UserInfoResponBean;
import com.ewanghuiju.app.model.prefs.ImplPreferencesHelper;
import com.ewanghuiju.app.presenter.login.PhoneLoginPresenter;
import com.ewanghuiju.app.ui.main.activity.MainActivity;
import com.ewanghuiju.app.ui.web.MyWebviewActivity;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.StartActivityUtil;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.ewanghuiju.app.util.sharemoreimage.Tools;
import com.gyf.immersionbar.ImmersionBar;
import com.muddzdev.styleabletoast.StyleableToast;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareConfig;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

import androidx.appcompat.widget.AppCompatCheckBox;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author sj  qq:751943509
 * @class 登录首页
 * @time 2020/2/11
 */
public class LoginMainActivity extends BaseActivity<PhoneLoginPresenter> implements PhoneLoginContract.View {

    @BindView(R.id.layout_wx_login)
    LinearLayout layoutWxLogin;
    @BindView(R.id.layout_phone_login)
    LinearLayout layoutPhoneLogin;
    @BindView(R.id.tv_yhxy)
    TextView tvYhxy;
    @BindView(R.id.tv_yszc)
    TextView tvYszc;
    @BindView(R.id.layout_xieyi)
    LinearLayout layoutXieyi;
    @BindView(R.id.checkbox)
    AppCompatCheckBox checkbox;

    @Override
    protected int getLayout() {
        return R.layout.activity_login_main;
    }


    @OnClick({R.id.layout_wx_login, R.id.layout_phone_login, R.id.tv_yhxy, R.id.tv_yszc})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.layout_wx_login:
                //微信登录
                if (!checkbox.isChecked()) {
                    StyleableToast.makeText(mContext, "请选择用户协议", Toast.LENGTH_SHORT, R.style.mytoast).show();
                    return;
                }
                getWxInfo();
                break;
            case R.id.layout_phone_login:
                if (!checkbox.isChecked()) {
                    StyleableToast.makeText(mContext, "请选择用户协议", Toast.LENGTH_SHORT, R.style.mytoast).show();
                    return;
                }
                new StartActivityUtil(mContext, PhoneLoginActivity.class).putExtra(Constants.SETTING_TYPE, 1)
                        .startActivity(true);
                break;
            case R.id.tv_yhxy:
                //用户协议
                new StartActivityUtil(mContext, MyWebviewActivity.class).putExtra("url", Constants.WEBURL_USERXY)
                        .putExtra(Constants.WEBURL_TITLE, "用户协议").startActivity(true);
                break;
            case R.id.tv_yszc:
                //隐私政策
                new StartActivityUtil(mContext, MyWebviewActivity.class).putExtra("url", Constants.WEBURL_USERXY)
                        .putExtra(Constants.WEBURL_TITLE, "隐私政策").startActivity(true);
                break;
            default:
                break;
        }
    }

    private String mUnionid, mNickname, mHeadimgurl;

    public void getWxInfo() {
        if (!Tools.isAppAvilible(mContext, "com.tencent.mm")) {
            StyleableToast.makeText(mContext, "您还没有安装微信", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
        UMShareAPI api = UMShareAPI.get(this);
        UMShareConfig config = new UMShareConfig();
        config.isNeedAuthOnGetUserInfo(true);
        api.setShareConfig(config);
        api.getPlatformInfo(mContext, SHARE_MEDIA.WEIXIN, new UMAuthListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {
                LoadingDialogUtils.show(mContext);
            }

            @Override
            public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
                if (map == null || TextUtils.isEmpty(map.get("unionid"))) {
                    LoadingDialogUtils.dismissDialog_ios();
                    StyleableToast.makeText(mContext, "获取用户信息失败", Toast.LENGTH_SHORT, R.style.mytoast).show();
                    return;
                }
                mUnionid = map.get("unionid");
                mNickname = map.get("name");
                mHeadimgurl = map.get("iconurl");
                mPresenter.wxUserLogin(mUnionid);
            }

            @Override
            public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
                LoadingDialogUtils.dismissDialog_ios();
            }

            @Override
            public void onCancel(SHARE_MEDIA share_media, int i) {
                LoadingDialogUtils.dismissDialog_ios();
            }
        });

    }

    @Override
    protected void initEventAndData() {
        finishAnim = false;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .titleBar(mToolbar, true)
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void showLogin(LoginResponBean bean) {
        //登录成功
        if (bean != null && !TextUtils.isEmpty(bean.getIs_bind_mobile()) && "1".equals(bean.getIs_bind_mobile())) {
            if (!TextUtils.isEmpty(bean.getToken())) {
                ImplPreferencesHelper mImplPreferencesHelper = App.getAppComponent().preferencesHelper();
                mImplPreferencesHelper.setToken(bean.getToken());
                //获取用户信息  到达首页
                mPresenter.getUserInfo();
            }
        } else {
            LoadingDialogUtils.dismissDialog_ios();
            //绑定用户信息
            new StartActivityUtil(mContext, CodeLoginActivity.class).putExtra(Constants.BIND_USERINFO, 2)
                    .putExtra(Constants.BIND_UNIONID, mUnionid)
                    .putExtra(Constants.BIND_NICKNAME, mNickname)
                    .putExtra(Constants.BIND_HEADIMGURL, mHeadimgurl)
                    .startActivity(true);
        }
    }

    @Override
    public void showLoginResponseMsg(int code, String message) {

    }

    @Override
    public void showUserInfo(UserInfoResponBean bean) {
        LoadingDialogUtils.dismissDialog_ios();
        StyleableToast.makeText(mContext, "登录成功", Toast.LENGTH_SHORT, R.style.mytoast).show();
        new StartActivityUtil(mContext, MainActivity.class)
                .startActivity(false);
        onBackPressedSupport();
        slideRightIn();
    }

    @Override
    public void showUserInfoEorrMsg(int code, String message) {

    }
}
